package com.yuanli.cosmeticmirror.persenter;

import androidx.lifecycle.LifecycleOwner;

import com.comenjoysoft.baselibrary.listener.CallbackListener;
import com.comenjoysoft.baselibrary.presenter.BasePresenter;
import com.orhanobut.logger.Logger;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UpProgressHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.yuanli.cosmeticmirror.bean.UnifyBean;
import com.yuanli.cosmeticmirror.model.biz.IFeedBackBiz;
import com.yuanli.cosmeticmirror.model.impl.FeedbackImpl;
import com.yuanli.cosmeticmirror.model.iview.IFeedBackView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class FeedbackPersenter extends BasePresenter<IFeedBackView> {
    private final IFeedBackBiz biz;
    private final UploadManager manager;

    public FeedbackPersenter(IFeedBackView view, LifecycleOwner owner) {
        attachView(view);
        biz = new FeedbackImpl(owner);
        manager = new UploadManager();
    }


    public void getToken(String describe, String imgUrl) {
        biz.getToken(new CallbackListener<String>() {
            @Override
            public void success(String object) {
                Logger.d("uploadToQi  :" );
                uploadToQi(object, imgUrl, describe);
            }

            @Override
            public void failed(Object object) {
                mView.onError();
            }
        });
    }

    /**
     * 上传图片到七牛云
     *
     * @param object
     * @param imgUrl
     * @param describe
     */
    private void uploadToQi(String object, String imgUrl, String describe) {
        UploadOptions opt = new UploadOptions(null, null, true, new UpProgressHandler() {
            @Override
            public void progress(String key, double percent) {
                Logger.d("qiniutest", "percent:" + percent);
            }
        }, null);
        File uploadFile = new File(imgUrl);
        manager.put(uploadFile, uploadFile.getName(), object, new UpCompletionHandler() {
            @Override
            public void complete(String key, ResponseInfo info, JSONObject response) {
                if (response==null){
                    mView.onError();
                    return;
                }
                Logger.d("uploadToQi response" + response.toString());
                Logger.d("uploadToQi key" + key.toString());
                try {
                    String fileHash = response.getString("key");
                    upload(describe, "http://res.dreamyin.cn/"+fileHash);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, opt);
    }

    public void upload(String describe, String imgUrl) {
        biz.uploadImg(describe, imgUrl, new CallbackListener<UnifyBean>() {
            @Override
            public void success(UnifyBean object) {
                mView.onSucess(object.getCode());
            }

            @Override
            public void failed(Object object) {
                mView.onError();
            }
        });
    }
}
